const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {
    CleanWebpackPlugin
} = require("clean-webpack-plugin");
module.exports = {

    mode: "production",
    // 开发模式
    // mode: "development",
    entry: {
        app: './src/index.js',
    },
   
    devServer: {
        contentBase: path.resolve(__dirname, "dist"),
        port: "8081",
        proxy: {
       "/api": {
         target: "http://localhost:3001",
         pathRewrite: {
           "^/api": ""
         }
       },
       "/resource": {
         target: "http://localhost:3002",
         pathRewrite: {
           "^/resource": ""
         }
       }
     }
    },
    
    output: {
        filename: "[name].[chunkhash].bundle.js",

        path: path.resolve(__dirname, "dist")
    },
    plugins: [
        new CleanWebpackPlugin({
            cleanOnceBeforeBuildPatterns: ["!lib/*"]}
        ),
        new HtmlWebpackPlugin({
            title: "Output Management",
            title: "Output Management",
            template: "./index.html",

        }),
        new MiniCssExtractPlugin({
            filename: "[name]-[contenthash].css"
        })

    ],
    module: {

        rules: [{
                test: /\.m?js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.css$/,

                use: [MiniCssExtractPlugin.loader, "css-loader"]
            },
            {
                test: /\.(png|svg|jpg|gif|webp)$/,
                use: [
                    'file-loader',
                ],
            },


        ]
    }
};